package site.luojie.sss.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Description: 安全校验拦截器
 * @Author jie.luo
 * @Create: 2020-05-19 23:55
 **/
public class SecurityInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        Object username = request.getSession().getAttribute("username");

        // 检测sessionId是否合法
        if (null == username || "".equals(username.toString().trim())) {
            response.getWriter().write("401 unauthorized");
            return false;
        }

        return true;
    }
}
